﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{
    public partial class MainService : IInterestRate
    {
        public int InterestRate_Using(int InterestRateID)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_InterestRate_Using", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@InterestRateID", InterestRateID);
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet InterestRate_GetAll()
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_InterestRate_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet InterestRate_GetMaxInterest()
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_InterestRate_GetMaxInterest", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet InterestRate_GetList(out int ItemCount, int currentPage, int pageSize)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_InterestRate_GetList", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public InterestRateInfo InterestRate_GetInfo(int InterestRateID)
        {
            InterestRateInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_InterestRate_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@InterestRate_ID", InterestRateID);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new InterestRateInfo();
                    retVal.ID = Convert.ToInt32(dr["InterestRate_ID"]);
                    retVal.Code = dr["InterestRate_Code"].ToString();
                    retVal.FromDate = dr["InterestRate_FromDate"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["InterestRate_FromDate"]);
                    retVal.ToDate = dr["InterestRate_ToDate"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["InterestRate_ToDate"]);
                    retVal.Percent = Convert.ToDecimal(dr["InterestRate_Percent"]);
                    retVal.Description = dr["InterestRate_Description"].ToString();
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public int InterestRate_Insert(InterestRateInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_InterestRate_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@InterestRate_Code", eInfo.Code);
            dbCmd.Parameters.AddWithValue("@InterestRate_FromDate", eInfo.FromDate);
            dbCmd.Parameters.AddWithValue("@InterestRate_ToDate", eInfo.ToDate);
            dbCmd.Parameters.AddWithValue("@InterestRate_Percent", eInfo.Percent);
            dbCmd.Parameters.AddWithValue("@InterestRate_Description", eInfo.Description);

            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool InterestRate_Update(InterestRateInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_InterestRate_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@InterestRate_ID", eInfo.ID);
            dbCmd.Parameters.AddWithValue("@InterestRate_Code", eInfo.Code);
            dbCmd.Parameters.AddWithValue("@InterestRate_FromDate", eInfo.FromDate);
            dbCmd.Parameters.AddWithValue("@InterestRate_ToDate", eInfo.ToDate);
            dbCmd.Parameters.AddWithValue("@InterestRate_Percent", eInfo.Percent);
            dbCmd.Parameters.AddWithValue("@InterestRate_Description", eInfo.Description);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool InterestRate_Delete(int InterestRateID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_InterestRate_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@InterestRate_ID", InterestRateID);

            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}

